<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form v-loading="showLoading" element-loading-text="上传中" element-loading-spinner="el-icon-loading" ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="状态">
            <!-- <el-input v-model="form.status" style="width: 370px;" /> -->
            <el-select v-model="form.status" placeholder="请选择" style="width: 370px;">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="链接">
            <div style="display:flex">
              <el-input v-model="form.url" style="margin-right: 5px" />
              <el-upload
                ref="upload"
                :limit="1"
                :headers="headers"
                :show-file-list="false"
                :before-upload="beforeUpload"
                :on-success="handleSuccess"
                :on-error="handleError"
                :action="baseApi + '/front/upload/uploadV1'"
              >
                <el-button type="primary">上传图片</el-button>
              </el-upload>
            </div>
            <el-image v-if="form.url" :src="form.url" style="width: 150px;height:150px;"></el-image>
          </el-form-item>
          <el-form-item label="内容">
            <el-input v-model="form.content" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="标题">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2 || showLoading" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="status" label="状态" >
          <template slot-scope="scope">
             {{scope.row.status=='0' ? "正常" : scope.row.status=='1' ? '异常' : '错误'}}
          </template>
        </el-table-column>
        <el-table-column prop="url" label="链接" >
          <template slot-scope="scope">
            <el-image class="url" v-if="scope.row.url"  :src="scope.row.url"></el-image>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="ip" label="上传ip" />
        <el-table-column prop="createdAt" label="创建时间" />
        <el-table-column prop="updatedAt" label="修改时间" />
        <el-table-column v-if="checkPer(['admin','dccDuchachaXiangguan:edit','dccDuchachaXiangguan:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDccDuchachaXiangguan from '@/api/dccDuchachaXiangguan'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

const defaultForm = { id: null, status: null, type: null, url: null, content: null, title: null, outUrl: null, ip: null, operator: null, createdAt: null, updatedAt: null }
export default {
  name: 'DccDuchachaXiangguan',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '毒品知识普及', url: 'api/dccDuchachaXiangguan', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDccDuchachaXiangguan }})
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      options: [{
        value: 1,
        label: '异常'
      }, {
        value: 0,
        label: '正常'
      }],
      permission: {
        add: ['admin', 'dccDuchachaXiangguan:add'],
        edit: ['admin', 'dccDuchachaXiangguan:edit'],
        del: ['admin', 'dccDuchachaXiangguan:del']
      },
      showLoading: false,
      rules: {
      }    }
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    beforeUpload (file) {
      let list = ['jpeg','png','jpeg','dds','psd','pdt','webp','xmp','gif','bmp','svg','tiff']
      let type = file.type.split('/')[1] ? file.type.split('/')[1] : ''
      if (list.indexOf(type) === -1) {
        this.$message.error('文件类型不正确')
        return false
      }
      this.showLoading = true
    },
    handleSuccess (response,file,fileList) {
      if (response && response.imgUrl) {
        this.form.url = response.imgUrl
        this.$refs.upload.clearFiles()
        this.showLoading = false
        this.$message.success('上传成功')
      }
    },
    handleError () {
        this.showLoading = false
        this.$message.error('上传失败')
    }
  }
}
</script>

<style scoped>
.url{
  width: 80px;
  height: 80px;
  margin-bottom:-8px;
}
</style>
